﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using BENBUN.Utils;
using BENBUN.WIW;
using ZoneFiveSoftware.Common.Data.Measurement;
using ZoneFiveSoftware.SportTracks.Data;

namespace BENBUN.UI.Controls
{
    public partial class WIWLogEntry : UserControl
    {
        private WIWLogEntryData _instanceLog;
        private bool _ownData = false;

        public WIWLogEntry()
        {
            InitializeComponent();
        }

        public WIWLogEntry(WIWLogEntryData logdata, bool owndata, int controlWidth)
        {
            InitializeComponent();

            this.Width = controlWidth;
            _instanceLog = logdata;
            _ownData = owndata;

            paintActivyData();
        }

        private void paintActivyData()
        {
            paintIcons();

            label_username.Text = _instanceLog.username;

            switch (_instanceLog.icon)
            {
                case "1":
                    pictureBox1.Image = BENBUN.Properties.Resources._4_pin_any;
                    break;
                case "2":
                    pictureBox1.Image = BENBUN.Properties.Resources._4_pin_running;
                    break;
                case "3":
                    pictureBox1.Image = BENBUN.Properties.Resources._4_pin_cycling;
                    break;
                case "4":
                    pictureBox1.Image = BENBUN.Properties.Resources._4_pin_hiking;
                    break;
                case "5":
                    pictureBox1.Image = BENBUN.Properties.Resources._4_pin_rowing;
                    break;
                case "6":
                    pictureBox1.Image = BENBUN.Properties.Resources._4_pin_swimming;
                    break;
                case "7":
                    pictureBox1.Image = BENBUN.Properties.Resources._4_pin_skiing;
                    break;
                default:
                    pictureBox1.Image = BENBUN.Properties.Resources._4_pin_any;
                    break;
            }
            Length.Units distanceUnits = Plugin.GetApplication().SystemPreferences.DistanceUnits;
            Length.Units elevationUnits = Plugin.GetApplication().SystemPreferences.ElevationUnits;
            label_distance.Text = Length.ToString(Length.Convert(_instanceLog.distance, Length.Units.Meter, distanceUnits), distanceUnits, "N" + Length.DefaultDecimalPrecision(distanceUnits).ToString() + "u");

            string valAsc = Length.ToString(Length.Convert(_instanceLog.climbUp, Length.Units.Meter, elevationUnits), elevationUnits, "N" + Length.DefaultDecimalPrecision(elevationUnits).ToString() + "u");
            string valDesc = Length.ToString(Length.Convert(_instanceLog.climbDown, Length.Units.Meter, elevationUnits), elevationUnits, "N" + Length.DefaultDecimalPrecision(elevationUnits).ToString() + "u");

            int t = _instanceLog.duration;
            int t_days = 0;
            int t_hours = 0;
            int t_minutes = 0;
            int t_seconds = 0;

            if (t > (24 * 60 * 60))
            {
                t_days = Math.DivRem(t, (24 * 60 * 60), out t);
            }
            if (t > (60 * 60))
            {
                t_hours = Math.DivRem(t, (60 * 60), out t);
            }
            if (t > (60))
            {
                t_minutes = Math.DivRem(t, (60), out t_seconds);
            }
            label_duration.Text = String.Format("{0:##} {1:00}:{2:00}:{3:00}", t_days, t_hours, t_minutes, t_seconds);
            label_category.Text = _instanceLog.category;
            //string format = @"M/d/yyyy H:m:s zzz";

            label_datetime.Text = _instanceLog.activityDate.Date.ToLongDateString();
            label_climb.Text = valAsc + " / -" + valDesc;
        }

        private void paintIcons()
        {
            if (_ownData)
            {
                buttonFriend.Visible = false;
                label_username.Visible = false;
                label4.Visible = false;
                
            }
            else
            {
                this.changeActivityCategoryToolStripMenuItem.Enabled = false;
                this.removeFromWIWToolStripMenuItem.Enabled = false;

            }
        }

        private void removeFromWIWToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Visible = false;
            Controller.GetInstance().purgeActivity(_instanceLog.uid);
        }

        private void setActivityType(object sender, EventArgs e)
        {
            ToolStripMenuItem menuSelected = (ToolStripMenuItem)sender;
            int newIcon = 0;
            switch (menuSelected.Name)
            {
                case "otherToolStripMenuItem":
                    pictureBox1.Image = BENBUN.Properties.Resources._4_pin_any;
                    newIcon = 0;
                    break;
                case "runningToolStripMenuItem":
                    pictureBox1.Image = BENBUN.Properties.Resources._4_pin_running;
                    newIcon = 2;
                    break;
                case "cyclingToolStripMenuItem":
                    pictureBox1.Image = BENBUN.Properties.Resources._4_pin_cycling;
                    newIcon = 3;
                    break;
                case "walkingToolStripMenuItem":
                    pictureBox1.Image = BENBUN.Properties.Resources._4_pin_hiking;
                    newIcon = 4;
                    break;
                case "rowingToolStripMenuItem":
                    pictureBox1.Image = BENBUN.Properties.Resources._4_pin_rowing;
                    newIcon = 5;
                    break;
                case "swimmingToolStripMenuItem":
                    pictureBox1.Image = BENBUN.Properties.Resources._4_pin_swimming;
                    newIcon = 6;
                    break;
                case "skiingToolStripMenuItem":
                    pictureBox1.Image = BENBUN.Properties.Resources._4_pin_skiing;
                    newIcon = 7;
                    break;
                default:
                    newIcon = 0;
                    break;
            }

            Controller.GetInstance().setIcon(this._instanceLog.uid,newIcon);
        }

    }
}